package gui.batches;

import gui.common.DataConverter;
import gui.product.ProductData;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TreeSet;

import coreModel.Barcode;
import coreModel.HIT;
import coreModel.Item;


/**
 * Controller class for the remove item batch view.
 */
public class RemoveItemBatchController extends ItemBatchController implements
IRemoveItemBatchController
{
	/**
	 * Constructor.
	 * 
	 * @param view Reference to the remove item batch view.
	 */
	public RemoveItemBatchController(ItemBatchView view) 
	{
		super(view, null);
		
		construct();
	}

	/**
	 * Returns a reference to the view for this controller.
	 */
	@Override
	protected RemoveItemBatchView getView() 
	{
		return (RemoveItemBatchView)super.getView();
	}

	/**
	 * This method is called when the "Item Barcode" field is changed
	 * in the remove item batch view by the user.
	 */
	@Override
	public void barcodeChanged() 
	{
		//added to automatically getting the barcode
		if(useScanner && notScheduled)
		{
			notScheduled=false;
			BarcodeReaderForRemoving br= new BarcodeReaderForRemoving();
			try
			{
				timer.schedule(br, BARCODE_TIME);
			}
			catch (IllegalStateException e) //should reach here iff timer was canceled
			{
				timer = new Timer("barCode thread");
				timer.schedule(br, BARCODE_TIME);
			}
		}
		else if(!useScanner)
		{
			enableComponents();
			timer.cancel();
		}
	}

	/**
	 * This method is called when the user clicks the "Remove Item" button
	 * in the remove item batch view.
	 */
	@Override
	public void removeItem() 
	{
		
		
		//find the item by its barcode
		Barcode iBarcode = new Barcode(getView().getBarcode());
		Item i = itemManager.getItem(iBarcode);
		if (i == null)
		{
			getView().displayErrorMessage("The specified item does not exist.");
			//clearing barcode
			getView().setBarcode("");
			return;
		}

		
		//get the item's container
		//ProductContainer oldParent = i.getParentContainer(); 
		//TODO: this should be "oldContainer=i.getP..." once in the Removal class

		ArrayList<Item> itemList = new ArrayList<Item>();
		
		itemList.add(i);
		
		redoStack.clear();
		
		
		Removal items = new Removal(itemList);
		items.redo();
		
		undoStack.push(items);
		//remove the item
		//itemFacade.removeItem(i);

		//clear barcode
		getView().setBarcode("");
		
		this.getView().giveBarcodeFocus();
		loadValues();
		//undoStack.push(new Removal(i));
		
		enableComponents();
		
		System.out.println(HIT.getInstance().getItemHistory().getAllProducts().size());
	}
	
	
	/**
	 * This method is called when the user clicks the "Redo" button
	 * in the add item batch view.
	 */
	@Override
	public void redo() 
	{
		
		redoStack.peek().redo();
		undoStack.push(redoStack.pop());
		loadValues();
		enableComponents();
			
	}

	/**
	 * This method is called when the user clicks the "Undo" button
	 * in the add item batch view.
	 */
	@Override
	public void undo() 
	{	
		
		undoStack.peek().undo();
		redoStack.push(undoStack.pop());
		loadValues();
		enableComponents();

	}
	
	
	/**
	 * Private class for keeping track of removals we might undo/redo
	 * in the remove item batch view.
	 */
	private class Removal extends Command
	{
		/**
		 * Constructor.
		 * 
		 * @param item The item to be removed.
		 */
		public Removal(ArrayList<Item> i){
			items = i;
		}
		
		
		
		public ArrayList<Item> getItems(){
			return items;
		}
		
		/**
		 * Executes this removal, whether for the first time or if it has been undone.
		 */
		public void redo()
		{												
			itemFacade.unaddItems(items);
		}
		
		
		/**
		 * Undoes (makes undone) this removal, and all of its effects. This includes 
		 * any product creation.
		 */
		@Override
		public void undo()
		{
			itemFacade.addItems(items);
		}
		
	}

	/** Private class for the barcode scanning timer in the remove item batch view. 
	 * 
	 */
	private class BarcodeReaderForRemoving extends BarcodeReader
	{
		/** 
		 * This will be called when this timer runs out. 
		 */
		@Override
		public void run()
		{
			if(getView().getBarcode().length() != 0) // change to check to enabled support
				removeItem();

			notScheduled=true;
			getView().setBarcode("");
		}

	}


}

